<?php

namespace App\Http\Controllers\V1\Admin;

use App\Http\Requests\Admin\UsersServer;
use App\Models\V1\Admins;
use App\Models\V1\Agent;
use App\Models\V1\Bank;
use App\Models\V1\Config;
use App\Models\V1\Games;
use App\Models\V1\SysPermission;
use App\Models\V1\SysRole;
use App\Models\V1\Users;
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AuditController extends BaseController
{


    /**
     * 增加用户
     *
     * @param UsersServer $request
     * @return \Illuminate\Http\Response
     */
    public function Add(UsersServer $request)
    {
        $request->validate('v1.create');

        $mobile = $request->input('mobile', "");
        $existingUser = Users::where('mobile', $mobile)->first();
        if ($existingUser) {
            return ajax_output(300, "此手机号已注册");
        }

        $model = new Users();
        $user = auth('box_admin')->user();
        $model->create([
            'preparatory_id' => $user['id'],
            'community_id' => $user['community_id'],
            'mobile' => $mobile,
            'real_name' => $request->input('real_name', ""),
            'username' => $request->input('username'),
            'partition' => $request->input('partition', ""),
            'element' => $request->input('element', ""),
            'building' => $request->input('building', ""),
            'floor' => $request->input('floor', ""),
            'hnum' => $request->input('hnum', ""),
            'area' => $request->input('area', ""),
            'state' => 1,
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAuditList(Request $request)
    {


        $query = Users::query();
        $user = auth('box_admin')->user();

        $query->where('preparatory_id', $user['id']);

        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');
        if ($request->filled('username')) {
            $query->where('username', 'like', '%' . $request->input('username') . '%');
        }
        if ($request->filled('mobile')) {
            $query->where('mobile', "like", "%" . $request->input('mobile') . "%");
        }

        if ($request->filled('id')) {
            $query->where('id', $request->input('id'));
        }

        if ($request->filled('state')) {
            $query->where('state', $request->input('state'));
        }
        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);


        return ajax_output(200, "成功", $data);
    }


    /**
     * 改变用户转态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeUserBankStatus(Request $request)
    {
        $users_id = $request->input('id', 0);
        $users_model = new Users();

        $vo = $users_model->find($users_id);
        if (!$vo) {
            return ajax_output(300, "数据不存在");
        }

        $state = $vo->state = (int)$request->input('status', 1);


        $vo->save();



        return ajax_output(200, "成功");



    }


    /**
     * 驳回用户个人认证
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function submitAudit(Request $request)
    {
        $id = $request->input('bankId', 0);//用户id
        $type = $request->input('type', 0);//0驳回 1通过

        try {
            DB::beginTransaction();
            $user_model = new Users();
            $vo_u = $user_model->find($id);
            if (!$vo_u) {
                return ajax_output(300, "数据不存在");
            }
            if ($type == 1) {
                $vo_u->audit_status = (int)$request->input('audit_status', 3);
                $vo_u->dev_class = 'P1';
                $settlement_ratio =  Config::query()->where('config_key','settlement_ratio')->value('config_value');
                $vo_u->settlement_ratio = $settlement_ratio;//结算比例


            } else {
                $vo_u->audit_status = (int)$request->input('audit_status', 4);
            }
            $vo_u->save();
            DB::commit();
            return ajax_output(200, "成功");

        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
    }


    /**
     * 用户详情
     * @param $id
     * @return void
     */
    public function getUserInfo($id)
    {
        $vo = Users::find($id);
        $user_model = new Users();
        $red = $user_model->where('id', $vo['users_id'])->get();
        $vo['info'] = $red;
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 删除代理商
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function Delete($id)
    {
        Users::destroy($id);
        return ajax_output(200, "成功",$id);
    }


    /**
     * 修改用户
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function updateUser(Request $request, $id)
    {
        try {
            DB::beginTransaction();

            $mobile = $request->input('mobile');
            $real_name = $request->input('real_name');
            $username = $request->input('username');
            $partition = $request->input('partition');
            $building = $request->input('building');
            $floor = $request->input('floor');
            $hnum = $request->input('hnum');
            $element= $request->input('element');
            $area= $request->input('area');
            $existingUser = Users::where('mobile', $mobile)->where('id', '!=', $id)->first();
            if ($existingUser) {
                DB::rollBack();
                return ajax_output(300, "手机号码已注册");
            }



            $data['mobile'] = $mobile;
            $data['real_name'] = $real_name;
            $data['username'] = $username;
            $data['partition'] = $partition;
            $data['building'] = $building;
            $data['floor'] = $floor;
            $data['hnum'] = $hnum;
            $data['element'] = $element;
            $data['area'] = $area;

            $voo = Users::find($id);
            $voo->update($data);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
        return ajax_output(200, "成功");

    }


}
